#!/usr/bin/perl -w

###############################################################################
# Load a set of pheno, geno, idlink, and anno files into the corresponding
# tables in the database.
#
# If the temp directory does not exist, it will be created and then deleted.
#
# Parameters needed: dbname, username, platform, study, temp_dir
#
# Author: Herodotos Herodotou and Nedyalko Borisov
# Date: March 11, 2010
#
##############################################################################

chdir("./src"); # Change the execution directory to src
use lib './src'; # Specify the library location
use getOpts qw(get_options println);

if ($#ARGV != 0)
{
   println qq(Please specify the .properties file);
   println qq(Usage: perl $0 load.properties);
   exit(-1);
}

# Get input parameters
%opt = get_options($ARGV[0]);
$DB_NAME  =$opt{'dbname'};
$USERNAME =$opt{'username'};
$STUDY = $opt{'study'};
$PLATFORM = $opt{'platform'};
$TEMP_DIR = $opt{'temp_dir'};

# Check the platform
if ($PLATFORM ne "illumina" && $PLATFORM ne "affymetrix")
{
   println "ERROR: The only supported platforms are illumina and affymetrix";
   exit(-1);
}

# Create the temp directory
if (! -d "$TEMP_DIR")
{
   mkdir $TEMP_DIR or die $!;
}

# Check this is a new study
$sql = "select study_id from study_platform where study_id = '$STUDY'";
!system qq(psql -U $USERNAME -d $DB_NAME -c "$sql" -t -o $TEMP_DIR/check_study) or die $!;
open INFILE, "<", "$TEMP_DIR/check_study" or die $!;
if (<INFILE> =~ m/$STUDY/)
{
   println "ERROR: The study $STUDY already exists in the database";
   close INFILE;
   system qq(rm -f $TEMP_DIR/check_study);
   exit(-1);
}
close INFILE;
system qq(rm -f $TEMP_DIR/check_study);

# Transform and load pheno data
$start = time();
!system qq(perl transform_pheno.pl $ARGV[0]) or die $!;
!system qq(perl load_pheno.pl $ARGV[0]) or die $!;
println qq();
$pheno_time = time() - $start;

# Transform and load study metadata
!system qq(perl load_study.pl $ARGV[0]) or die $!;


# Transform and load idlink data
$start = time();
!system qq(perl transform_idlink.pl $ARGV[0]) or die $!;
!system qq(perl load_idlink.pl $ARGV[0]) or die $!;
println qq();
$idlink_time = time() - $start;

# Transform and load anno data
$start = time();
if ($PLATFORM eq "illumina")
{
   !system qq(perl transform_anno_illum.pl $ARGV[0]) or die $!;
}
else
{
   !system qq(perl transform_anno_affy.pl $ARGV[0]) or die $!;
}
!system qq(perl load_anno.pl $ARGV[0]) or die $!;
println qq();
$anno_time = time() - $start;

# Transform and load geno data
$start = time();
if ($PLATFORM eq "illumina")
{
   !system qq(perl transform_geno_illum.pl $ARGV[0]) or die $!;
}
else
{
   !system qq(perl transform_geno_affy.pl $ARGV[0]) or die $!;
}
!system qq(perl load_geno.pl $ARGV[0]) or die $!;
println qq();
$geno_time = time() - $start;

# Remove the temp directory if empty
if (! scalar <$TEMP_DIR/*>)
{
   rmdir $TEMP_DIR;
}

# Print out timings
println qq(Time to load pheno (sec):\t $pheno_time);
println qq(Time to load idlink (sec):\t $idlink_time);
println qq(Time to load anno (sec):\t $anno_time);
println qq(Time to load geno (sec):\t $geno_time);
$total_time = $pheno_time + $idlink_time + $anno_time + $geno_time;
println qq(Total loading time (sec):\t $total_time);

